#!/bin/bash
echo 创建nodered服务
cat >/lib/systemd/system/nodered.service<<EOF
[Unit]
Description=Node-RED
Wants=network.target

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/root

Nice=5
Environment="NODE_OPTIONS=--max_old_space_size=256"
ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
KillSignal=SIGINT
Restart=on-failure
SyslogIdentifier=Node-RED
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target
EOF
echo 自定义日志路径
cat >>/etc/rsyslog.d/nodered.conf<<EOF
if \$programname == 'Node-RED' then /root/logs/nodered.log
& stop
EOF

echo 安装nodered
#把python3的软链切换到系统自带的python3.7,像hass或omv等软件会把python3的软链改为python3.8,会引起nodered安装出错
rm -f /usr/local/bin/python3
ln -s /usr/bin/python3.7 /usr/local/bin/python3
#下载安装脚本并安装
wget https://gitee.com/base/linux-installers/raw/master/deb/update-nodejs-and-nodered
bash update-nodejs-and-nodered
echo 注册服务
systemctl daemon-reload
systemctl restart rsyslog
systemctl enable nodered
rm -f update-nodejs-and-nodered
echo nodered安装完成